package com.sky.sd.system.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.mysql.cj.util.StringUtils;
import com.netflix.discovery.converters.Auto;
import com.sky.sd.common.web.response.Result;
import com.sky.sd.system.bean.dto.DeptNode;
import com.sky.sd.system.bean.dto.MenuNode;
import com.sky.sd.system.bean.dto.UserDto;
import com.sky.sd.system.bean.po.Dept;
import com.sky.sd.system.bean.po.User;
import com.sky.sd.system.bean.vo.*;
import com.sky.sd.system.service.IDeptService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 部门 前端控制器
 * </p>
 *
 * @author 弓振
 * @since 2020-07-03
 */
@RestController
@RequestMapping("/system/dept")
public class DeptController {

    @Autowired
    private IDeptService deptService;

    @GetMapping("/list")
    @ApiOperation(value = "查询所有部门列表")
    public Result listDept(DeptSearchVo vo) {
        IPage<DeptNode> page = deptService.listDeptPage(vo);
        return Result.success(page);
    }

    @GetMapping("/check/deptName")
    @ApiOperation(value = "验证部门名称是否重复")
    public Result listDept(String id,String deptName) {
        int cnt = deptService.count(Wrappers.<Dept>lambdaQuery()
                .ne(!StringUtils.isNullOrEmpty(id), Dept::getId, id).eq(Dept::getDeptName, deptName));
        return Result.success(cnt);
    }

    @GetMapping("/check/parentId")
    @ApiOperation(value = "验证当前选择的父部门是否是子节点或当前节点")
    public Result checkParentId(String parentId,String id) {
        int cnt = deptService.checkParentId(parentId, id);
        return Result.success(cnt);
    }

    @GetMapping("/get")
    @ApiOperation(value = "查询部门列表")
    public Result getDept(String id) {
        Dept dept = deptService.getById(id);
        return Result.success(dept);
    }

    @PostMapping("/add")
    @ApiOperation(value = "新增部门")
    public Result addDept(@RequestBody DeptAddVo vo) {
        deptService.add(vo);
        return Result.success();
    }

    @PostMapping("/update")
    @ApiOperation(value = "修改部门")
    public Result updateDept(@RequestBody DeptUpdateVo vo) {
        deptService.update(vo);
        return Result.success();
    }

    @PostMapping("/update/enabled")
    @ApiOperation(value = "修改部门状态")
    public Result updateDeptEnabled(@RequestBody DeptUpdateVo vo) {
        deptService.updateEnabled(vo);
        return Result.success();
    }

    @PostMapping("/delete")
    @ApiOperation(value = "删除部门")
    public Result deleteUser(@RequestBody DeleteVo vo) {
        deptService.delete(vo);
        return Result.success();
    }
}

